/**
 * ReportGear(2011)
 */
package com.reportgear.report.print.pane;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JPanel;

import net.abbp.swing.util.ImageUtils;

/**
 * 设置预览
 * 
 * @version 1.0 2011-4-20
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ShowPagePane extends JPanel {
	private double paperWidth;
	private double paperHeight;
	private int paperOrientation;
	private int paneWidth = 300;
	private int paneHeight = 100;
	private double lengthScale = 0.3D;
	private Image img;

	public ShowPagePane() {
		this.setSize(this.paneWidth, this.paneHeight);
		this.setPreferredSize(new Dimension(this.paneWidth, this.paneHeight));
		// this.setBackground(new Color(128, 128, 128));
		this.setOpaque(false);
		this.img = ImageUtils.readImage("dialog/pagesetup/a.png");
	}

	public void populate(double width, double height, int orientation, boolean scale) {
		if ((width <= 0.0D) || (height <= 0.0D))
			return;
		this.paperWidth = Math.min(width, 2000.0D);
		this.paperHeight = Math.min(height, 2000.0D);
		this.paperOrientation = orientation;
		this.lengthScale = (scale ? 0.3D : 3.0D);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if ((this.paperWidth <= 0.0D) || (this.paperHeight <= 0.0D))
			return;

		double d3 = this.lengthScale * this.paperWidth / (this.paneWidth - 50);
		double d4 = this.lengthScale * this.paperHeight / (this.paneHeight - 30);

		double d1, d2;
		if ((d3 > 1.0D) || (d4 > 1.0D)) {
			double d5 = (d3 > d4) ? d3 : d4;
			d1 = this.lengthScale * this.paperWidth / d5;
			d2 = this.lengthScale * this.paperHeight / d5;
		} else {
			d1 = this.paperWidth * this.lengthScale;
			d2 = this.paperHeight * this.lengthScale;
		}

		Graphics2D g2 = (Graphics2D) g;
		FontMetrics localFontMetrics = g2.getFontMetrics();
		String str1 = "" + this.paperWidth;
		if (str1.indexOf(".") > 0)
			str1 = str1.substring(0, str1.indexOf(".") + 2);
		int i = localFontMetrics.stringWidth(str1);
		d1 = Math.max(d1, i + 26);
		String str2 = "" + this.paperHeight;
		if (str2.indexOf(".") > 0)
			str2 = str2.substring(0, str2.indexOf(".") + 2);
		int j = localFontMetrics.stringWidth(str2);
		d2 = Math.max(d2, j + 26);
		double d6 = (this.paneWidth - d1) / 2.0D;
		double d7 = (this.paneHeight - d2) / 2.0D;
		g2.translate(d6, d7);
		g2.setColor(Color.WHITE);
		g2.fill3DRect(0, 0, (int) d1, (int) d2, true);
		g2.setColor(Color.BLACK);
		g2.drawLine(0, -5, 0, -15);
		g2.drawLine((int) d1 - 1, -5, (int) d1 - 1, -15);
		g2.drawString(str1, (int) ((d1 - i) / 2.0D), -5);
		g2.drawLine(2, -10, (int) ((d1 - i) / 2.0D) - 5, -10);
		g2.drawLine((int) ((d1 + i) / 2.0D) + 5, -10, (int) d1 - 3, -10);
		g2.drawLine(2, -10, 7, -13);
		g2.drawLine(2, -10, 7, -7);
		g2.drawLine((int) d1 - 3, -10, (int) d1 - 8, -13);
		g2.drawLine((int) d1 - 3, -10, (int) d1 - 8, -7);
		g2.drawLine(-15, 0, -5, 0);
		g2.drawLine(-15, (int) d2 - 1, -5, (int) d2 - 1);
		g2.rotate(-Math.toRadians(90.0D));
		g2.drawString(str2, -(int) ((d2 + j) / 2.0D), -5);
		g2.rotate(Math.toRadians(90.0D));
		g2.drawLine(-10, 2, -10, (int) ((d2 - j) / 2.0D) - 5);
		g2.drawLine(-10, (int) ((d2 + j) / 2.0D) + 5, -10, (int) d2 - 3);
		g2.drawLine(-10, 2, -13, 7);
		g2.drawLine(-10, 2, -7, 7);
		g2.drawLine(-10, (int) d2 - 3, -13, (int) d2 - 8);
		g2.drawLine(-10, (int) d2 - 3, -7, (int) d2 - 8);

		if (this.paperOrientation == 0) {
			g2.drawImage(this.img, (int) ((d1 - this.img.getWidth(null)) / 2.0D),
					(int) ((d2 - this.img.getHeight(null)) / 2.0D), null);
		} else {
			g2.rotate(-Math.toRadians(90.0D));
			g2.drawImage(this.img, -(int) ((d2 + this.img.getWidth(null)) / 2.0D), (int) ((d1 - this.img
					.getHeight(null)) / 2.0D), null);
			g2.rotate(Math.toRadians(90.0D));
		}

		g2.translate(-d6, -d7);
	}
}